package domain.cms;

import domain.User;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.search.annotations.ContainedIn;
import org.hibernate.search.annotations.Indexed;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;

/**
 * Abstract class for CmsPage, CmsCategory and CmsSection.
 */
@Entity
@NamedQueries({
        @NamedQuery(name = "CmsPageNew.findByUrl",
                query = "FROM CmsPage WHERE attribute.url.url LIKE :url"),
        @NamedQuery(name = "CmsPageNew.findByOldUrl",
                query = "FROM CmsPage WHERE attribute.url.url LIKE :url"),
        @NamedQuery(name = "CmsPageNew.findByTitle",
                query = "FROM CmsPage WHERE attribute.title LIKE :title"),
        @NamedQuery(name = "CmsPageNew.findChildsByParentId",
                query = "FROM CmsPage WHERE parent.id = :parentId"),

        @NamedQuery(name = "CmsPageNew.findRoots",
                query = "FROM CmsPage WHERE parent.id is NULL"),
        @NamedQuery(name = "CmsPageNew.findPublishedSections",
                query = "FROM CmsPage WHERE parent.id is NULL")  // and attribute.published = TRUE
})
@Indexed
@XmlRootElement(name = "CmsPage")
public class CmsPage extends AbstractPage {
//   //section
//    @Column(name = "CATEGORY_CHILDREN")
//    @OneToMany(mappedBy = "section", fetch = FetchType.LAZY)
//    @Cascade(CascadeType.ALL)
//    List<CmsPage> categories = new ArrayList<CmsPage>();
//
//    //page
//    @Column(name = "CATEGORY")
//    @ManyToOne(fetch = FetchType.LAZY)
//    private CmsPage parentCategory;
//
//    //category

    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    @Cascade(CascadeType.ALL)
    List<CmsPage> childs = new ArrayList<CmsPage>();


    @ManyToOne(fetch = FetchType.LAZY)
    @ContainedIn
    CmsPage parent;

    @Override
    public void setTitle(String title) {
        super.setTitle(title);
        setUrlIfNull();//TODO mozna by to slo udelat lepe

    }

    public void setUrlIfNull() {
        if (getUrl() == null || getUrl().equals("") || getUrl().equals(" ")) {
            String s = getTitle();

            setUrl(s);
        }
    }

    public CmsPage(User user) {
        setUser(user);
    }

    public void addChild(CmsPage cmsPage) {
        getChilds().add(cmsPage);
    }

    @Override
    public String toString() {
        return getTitle();
    }

    public CmsPage() {
    }


    public List<CmsPage> getChilds() {
        return childs;
    }

    public void setChilds(List<CmsPage> childs) {
        this.childs = childs;
    }

    public CmsPage getParent() {
        return parent;
    }

    public void setParent(CmsPage parent) {
        this.parent = parent;
    }

    public boolean isPage() {
        if (this.getParent() != null) {
            if (this.getParent().getParent() != null) {
                return true;
            }
        }
        return false;
    }

    private Integer getLevel(CmsPage cmsPage) {
        Integer i = 1;
        if (cmsPage.getParent() != null) {
            i = i + getLevel(cmsPage.getParent());
        }
        return i;
    }

    public Integer getLevel() {
        Integer i = 0;
        if (this.getParent() != null) {
            i = i + getLevel(this.getParent());
        }
        return i;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CmsPage)) return false;
        if (!super.equals(o)) return false;

        CmsPage cmsPage = (CmsPage) o;

        if (parent != null ? !parent.equals(cmsPage.parent) : cmsPage.parent != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (childs != null ? childs.hashCode() : 0);
        result = 31 * result + (parent != null ? parent.hashCode() : 0);
        return result;
    }


//    public CmsPage getParentCategory() {
//        return parentCategory;
//    }
//
//    public void setParentCategory(CmsPage parentCategory) {
//        this.parentCategory = parentCategory;
//    }
}